package record;
import java.io.*;
import javax.microedition.rms.*;
/**
 *
 * @author mehmetrg
 */
public class CalculationPrefer implements Methods {

    private byte  temkinImsak,temkinGunes,temkinOgle,temkinAsr,temkinMagrib,temkinYatsi;
    private byte  calculationMethod;
    private double duskAngle,dawnAngle;
    private byte  ishaMinute;
    private byte  sectChoice;
    private RecordStore recordStore;
    private DataOutputStream outputStream;
    private DataInputStream inputStream;
    public boolean noRecords=true,  enableFixedIsha=false,   isItFirstTime=false;
    private byte estimationMethod,amountOfNight;
    private float baseLatitude;

    public CalculationPrefer() {
    }
    public void setCalculationMethod(byte calculationMethod) {
        this.calculationMethod = calculationMethod;
    }
     public void saveCalcPreferInfo() {
        		
		        try {
		            recordStore = RecordStore.openRecordStore("calPrefer", true);
		        } catch (RecordStoreException rsc) {
		            return;
		        }
		        ByteArrayOutputStream baos = new ByteArrayOutputStream();
		        outputStream = new DataOutputStream(baos);
		        try {
		            outputStream.writeByte(temkinImsak);
		            outputStream.writeByte(temkinGunes);
		            outputStream.writeByte(temkinOgle);
		            outputStream.writeByte(temkinAsr);
		            outputStream.writeByte(temkinMagrib);
		            outputStream.writeByte(temkinYatsi);

		            outputStream.writeByte(calculationMethod);
                            outputStream.writeDouble(dawnAngle);
		            outputStream.writeDouble(duskAngle);

                        
                            outputStream.writeByte(sectChoice);

                            outputStream.writeByte(estimationMethod);
                            outputStream.writeByte(amountOfNight);
                            outputStream.writeFloat(baseLatitude);
                            outputStream.writeBoolean(enableFixedIsha);
		            outputStream.writeByte(ishaMinute);


		            byte[] bytes = baos.toByteArray();
		            if (recordStore.getNumRecords() == 0)
		                recordStore.addRecord(bytes, 0, bytes.length);
		            else
		                recordStore.setRecord(1, bytes, 0, bytes.length);
		        } catch (Exception exception) {
		        } finally {
		            try {outputStream.close();} catch (Exception ex) {System.out.println(ex.getMessage());}
		            try {recordStore.closeRecordStore();} catch (Exception ex) {System.out.println(ex.getMessage());}
        }
     }

         public void loadCalcPreferInfo() {
	         recordStore = null;
	         inputStream = null;
	         try {
	 	    recordStore = RecordStore.openRecordStore("calPrefer", false);
	             byte[] bytes = recordStore.getRecord(1);
	             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
	             inputStream = new DataInputStream(bais);
                     noRecords=false;
        
	             temkinImsak=inputStream.readByte();
	             temkinGunes=inputStream.readByte();
	             temkinOgle=inputStream.readByte();
	             temkinAsr=inputStream.readByte();
	             temkinMagrib=inputStream.readByte();
	             temkinYatsi=inputStream.readByte();
	             
                     calculationMethod=inputStream.readByte();
	             dawnAngle=inputStream.readDouble();
                     duskAngle=inputStream.readDouble();
	             
                    

	             sectChoice=inputStream.readByte();

                     estimationMethod=inputStream.readByte();
                     amountOfNight=inputStream.readByte();
                     baseLatitude=inputStream.readFloat();
                     enableFixedIsha=inputStream.readBoolean();
                     ishaMinute= inputStream.readByte();



	         } catch (Exception ex) {
                  //Ramazana uygun temkinler dokunma
                     noRecords=true;
                     temkinImsak=-1;
                     temkinGunes=-6;
                     temkinOgle=7;
                     temkinAsr=5;
                     temkinMagrib=9;
                     temkinYatsi=2;

                     calculationMethod=TURKISH_RELIGOUS;

                     dawnAngle=-18;
                     duskAngle=-17;
                     
                     sectChoice=0;
                     isItFirstTime=true;

                     estimationMethod=NO_ESTIMATION;
                     amountOfNight=ONE_THIRD_OF_NIGHT;
                     baseLatitude=48.5f;
                     enableFixedIsha=false;
                     ishaMinute=90;

	         } finally {
	             try {inputStream.close();} catch (Exception ex) {System.out.println(ex.getMessage());}
	             try {recordStore.closeRecordStore();} catch (Exception ex) {System.out.println(ex.getMessage());}
	         }
    }

    public byte getTemkinImsak()  {
       if (noRecords==false)   return temkinImsak;
       if (noRecords && calculationMethod==TURKISH_RELIGOUS)  return temkinImsak;
       return 0;
    }

    public byte getTemkinGunes() {
       if (noRecords==false)   return temkinGunes;
       if (noRecords && calculationMethod==TURKISH_RELIGOUS)  return temkinGunes;
       return 0;
    }

    public byte getTemkinOgle() {
       if (noRecords==false)   return temkinOgle;
       if (noRecords && calculationMethod==TURKISH_RELIGOUS)  return temkinOgle;
       return 0;
    }

    public byte getTemkinAsr() {
       if (noRecords==false)   return temkinAsr;
       if (noRecords && calculationMethod==TURKISH_RELIGOUS)  return temkinAsr;
       return 0;
    }

    public byte getTemkinMagrib() {
       if (noRecords==false)   return temkinMagrib;
       if (noRecords && calculationMethod==TURKISH_RELIGOUS)  return temkinMagrib;
       return 0;
    }

    public byte getTemkinYatsi() {
       if (noRecords==false)   return temkinYatsi;
       if (noRecords && calculationMethod==TURKISH_RELIGOUS)  return temkinYatsi;
       return 0;
    }
    public boolean isFixedIsha() {
      if (noRecords&&calculationMethod==UMM_ALQURRA)return true;
      return  enableFixedIsha;
    }


    public double getDawnAngle() {
         switch (calculationMethod) {
            case TURKISH_RELIGOUS: dawnAngle=-18;
            return dawnAngle;
            case NORTH_AMERICA: dawnAngle=-15;
            return dawnAngle;
            case MUSLIM_LEAGUE: dawnAngle=-18;
            return dawnAngle;
            case EGYPT_SURVEY:dawnAngle=-19.5;
            return dawnAngle;
            case KARACHI:dawnAngle=-18;
            return dawnAngle;
            case UMM_ALQURRA:dawnAngle=-19;
            return dawnAngle;}
        return dawnAngle;
    }

    public double getDuskAngle() {
      
        switch (calculationMethod) {
            case TURKISH_RELIGOUS: duskAngle=-17;
            return duskAngle;
            case NORTH_AMERICA: duskAngle=-15;
            return duskAngle;
            case MUSLIM_LEAGUE: duskAngle=-17;
            return duskAngle;
            case EGYPT_SURVEY:duskAngle=-17.5;
            return duskAngle;
            case KARACHI:duskAngle=-18;
            return duskAngle;
            case UMM_ALQURRA:duskAngle=90;
            return duskAngle;
          }
        
    
        return duskAngle;
    }
    public byte getFixedIshaMin() {
         if (noRecords&&calculationMethod==UMM_ALQURRA) return 90;
        return ishaMinute;

    }
    public byte getSectChoice() {
        return sectChoice;
    }

    public byte getCalculationMethod() {
       return calculationMethod;
    }
    public boolean getIsItFirstTime() {
       return isItFirstTime;
    }

    public byte getAmountOfNight() {
        return amountOfNight;
    }

    public byte getEstimationMethod() {
        return estimationMethod;
    }

     public float getBaseLatitude() {
        return baseLatitude;
    }
   public void setCalculationParameters(            
           byte  temkinImsak,   
           byte temkinGunes,  
           byte temkinOgle, 
           byte temkinAsr, 
           byte temkinMagrib,
           byte temkinYatsi,  
           double dawnAngle,
           double duskAngle,
           byte  sectChoice
           )
   {       noRecords=false;
           this.temkinImsak=  temkinImsak;
           this.temkinGunes =temkinGunes;
           this.temkinOgle =temkinOgle;
           this.temkinAsr= temkinAsr;
           this.temkinMagrib =temkinMagrib;
           this.temkinYatsi= temkinYatsi;
          
           this.dawnAngle= dawnAngle;
           this.duskAngle =duskAngle;        
           
           this.sectChoice= sectChoice;
           this.isItFirstTime=false;
   }

    public void setEstimationParameters(byte estimationMethod, float baseLatitude, byte amountOfNight, boolean enableFixedIsha, byte ishaMinute)
   {       this.estimationMethod=estimationMethod;
           this.amountOfNight=amountOfNight;
           this.baseLatitude=baseLatitude;
           this.enableFixedIsha=enableFixedIsha;
           this.ishaMinute=ishaMinute;
   }
}
